﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

public partial class Captcha : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        string randomText = CaptchaImage.GenerateRandomText("0123456789", 4);
        HttpContext.Current.Session["CAPTCHATEXT"] = randomText;
        try
        {
            if (Session["CAPTCHATEXT"] == null) return;

            CaptchaImage _captcha = new CaptchaImage();
            _captcha.Text = Convert.ToString(Session["CAPTCHATEXT"]);
            _captcha.CaptBackColor = "white";
            _captcha.CaptFontColor = "blue";
            _captcha.Font = "宋体";

            _captcha.LineNoise = CaptchaImage.LineNoiseLevel.None;
            _captcha.Height = 21;
            _captcha.Width = 61;

            _captcha.BackgroundNoise = CaptchaImage.BackgroundNoiseLevel.Extreme;

            Bitmap b = _captcha.RenderImage();
            b.Save(Context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            b.Dispose();
            Response.ContentType = "image/jpeg";
            Response.StatusCode = 200;
            Response.Flush();
        }
        catch (Exception ce)
        {
            //Logger.WriteLog(ce);
        }
    }
}